var ji = new Image(); //内摇杆图片
var jo = new Image(); //外摇杆图片
var joystick = document.getElementById('joystick'); //画板
<<<<<<< HEAD

var leftoffsetx=getAbsoluteLeft("id_content_manual_left");
var leftoffsety=getAbsoluteTop("id_content_manual_left");
=======
var leftoffsetx = getAbsoluteLeft("id_content_manual_left");
var leftoffsety = getAbsoluteTop("id_content_manual_left");
>>>>>>> dev

var josize = joystick.height; //外摇杆大小
var jisize = josize * 0.5; //内摇杆大小
var centerX = leftoffsetx + josize / 2; //摇杆中心x坐标
var centerY = leftoffsety + josize / 2; //摇杆中心y坐标
<<<<<<< HEAD
var touchX = joystick.offsetLeft,
	touchY = joystick.offsetTop;
window.addEventListener('load', load, false);
var jc = joystick.getContext('2d'); //画布

//摇杆头应当移动到的位置
var jx = 0,
	jy = 0;

//图片加载完成时执行这俩函数
ji.onload = function() {
	
	jc.drawImage(ji, centerX - leftoffsetx - jisize / 2, centerY - leftoffsety - jisize / 2, jisize, jisize); //首次绘制内摇杆
}
jo.onload = function() {
	jc.drawImage(jo, centerX -leftoffsetx - josize / 2, centerY - leftoffsety - josize / 2, josize, josize);
=======
var touchX = joystick.offsetLeft,touchY = joystick.offsetTop;
window.addEventListener('load', load, true);
var jc = joystick.getContext('2d'); //画布

//摇杆头应当移动到的位置
var jx = 0,jy = 0;

//图片加载完成时执行这俩函数
ji.onload = function () {
    jc.drawImage(ji, centerX - leftoffsetx - jisize / 2, centerY - leftoffsety - jisize / 2, jisize, jisize); //首次绘制内摇杆
}
jo.onload = function () {
    jc.drawImage(jo, centerX - leftoffsetx - josize / 2, centerY - leftoffsety - josize / 2, josize, josize);
>>>>>>> dev
}

//绘图函数（绘制图形的时候就是用户观察到摇杆动了，所以取名是move）
function move() {
<<<<<<< HEAD
	
	

	jc.clearRect(centerX - leftoffsetx - josize / 2, centerY - leftoffsety - josize / 2, josize, josize); //清空画板
	jc.drawImage(jo, centerX - leftoffsetx - josize / 2, centerY - leftoffsety - josize / 2, josize, josize); //画底座
	jc.drawImage(ji, centerX - leftoffsetx - jisize / 2 + jx, centerY - leftoffsety - jisize / 2 + jy, jisize, jisize); //画摇杆头
	requestAnimationFrame(move); //下一次绘图
}
//ji.src = '../img/joystickin.png'; //加载图片
//jo.src = '../img/joystickout.png'; //加载图片
ji.src = '../img/joystickin.png'+'?t=' + Math.random(); //加载图片
jo.src = '../img/joystickout.png'+"?t=" + Math.random(); //加载图片
=======
    jc.clearRect(centerX - leftoffsetx - josize / 2, centerY - leftoffsety - josize / 2, josize, josize); //清空画板
    jc.drawImage(jo, centerX - leftoffsetx - josize / 2, centerY - leftoffsety - josize / 2, josize, josize); //画底座
    jc.drawImage(ji, centerX - leftoffsetx - jisize / 2 + jx, centerY - leftoffsety - jisize / 2 + jy, jisize, jisize); //画摇杆头
    requestAnimationFrame(move); //下一次绘图
}
//ji.src = '../img/joystickin.png'; //加载图片
//jo.src = '../img/joystickout.png'; //加载图片
ji.src = '../img/joystickin.png' + '?t=' + Math.random(); //加载图片
jo.src = '../img/joystickout.png' + "?t=" + Math.random(); //加载图片
>>>>>>> dev

//移动位置
var moveX = 0.0;
var moveY = 0.0;
var isRockerMove = 0;

//页面加载时执行该函数
function load() {
<<<<<<< HEAD
	document.addEventListener('touchstart', touch, false);
	document.addEventListener('touchmove', touch, false);
	document.addEventListener('touchend', touch, false);

	//加载的时候先把摇杆绘制出来再说
	move();
	var effectiveFinger = -1; //当前有效手指

	//触摸事件触发函数
	function touch(event) {
		var event = event || window.event;
                
            
		var oInp = document.getElementById("inp");
		//界面未显示时使触摸事件无效
		var pageManualShowState = "";
		pageManualShowState = $("#page_manual_m_manual").css("display");
		if(pageManualShowState == "none" || pageManualShowState == "" || pageManualShowState == undefined)
		{
                    
			
		}
		else
		{
			
			switch(event.type) {
				case "touchstart":
					//判断是否击中摇杆头
					//开启手动线程
	
				
					
					for(var i = 0; i < event.touches.length; i = i + 1) {
						if(Math.sqrt(Math.pow(event.touches[i].clientX - centerX - touchX, 2) +
								Math.pow(event.touches[i].clientY - centerY - touchY, 2)) <=
							josize / 2 - jisize / 2) {
							effectiveFinger = i;
	
        
							if(objGlobalVariable.rockerMotionCmd === 0)
							{
								
								objGlobalVariable.rockerMotionCmd=1;
								objGlobalVariable.intervalRockerSendCmd=setInterval(function(){sendRockerMotionCmd()},100);
							}
							else
							{
								
								stopManualTimer(e);
								objGlobalVariable.rockerMotionCmd=1;
								objGlobalVariable.intervalRockerSendCmd=setInterval(function(){sendRockerMotionCmd()},100);
							}
	
							isRockerMove = 1;
						}
					}
					break;
				case "touchend": //手指离开的时候
					//若有效手指离开,那就把内摇杆放中间
					if(event.touches[effectiveFinger] == null) {
						if(event.touches[0] == null) {
							jx = 0;
							jy = 0;
						}
						effectiveFinger -= 1;
	
						stopRockerManualTimer();
						
	
						//var msg = " x " + 0 + " y "+0;
						//	document.getElementById("showmsg").textContent=msg;
					}
					break;
				case "touchmove": //手指移动的时候：
					//是否触摸点在摇杆上
	
					
					if(effectiveFinger != -1)
						if(Math.sqrt(Math.pow(event.touches[effectiveFinger].clientX - centerX - touchX, 2) +
								Math.pow(event.touches[effectiveFinger].clientY - centerY - touchY, 2)) <=
							josize / 2 - jisize / 2) {
							jx = event.touches[effectiveFinger].clientX - centerX - touchX;
							jy = event.touches[effectiveFinger].clientY - centerY - touchY;
	
							var currentTouchX = event.touches[effectiveFinger].clientX - centerX;
							var currentTouchY = event.touches[effectiveFinger].clientY - centerY;
							var manualValueX=currentTouchX/josize*1.0*2.0;
							var manualValueY=currentTouchY/josize*1.0*2.0;
	
							if(manualValueX > 1.0)
							{
								manualValueX = 1.0;
							}
							else if(manualValueX < -1.0)
							{
								manualValueX = -1.0;
							}
	
							if(manualValueY > 1.0)
							{
								manualValueY = 1.0;
							}
							else if(manualValueY < -1.0)
							{
								manualValueY = -1.0;
							}
	
							moveX = manualValueX*objGlobalVariable.objCusUpdateTopState.variable_local_manual_v/100.0;
							moveY = manualValueY*objGlobalVariable.objCusUpdateTopState.variable_local_manual_v/100.0;
	
							moveX = moveX.toFixed(2);
							moveY = moveY.toFixed(2);
	
							isRockerMove = 1;
	
							
	
							//var msg = currentTouchX.toFixed(2)+" " + currentTouchY.toFixed(2)+" " + manualValueX.toFixed(2) + " "+manualValueY.toFixed(2);
							//document.getElementById("showmsg").textContent=msg;
						} else
						//否则计算摇杆最接近的位置
						{
							var x = event.touches[effectiveFinger].clientX - touchX,
								y = event.touches[effectiveFinger].clientY - touchY,
								r = josize / 2 - jisize / 2;
	
							var ans = GetPoint(centerX, centerY, r, centerX, centerY, x, y);
							//圆与直线有两个交点，计算出离手指最近的交点
							if(Math.sqrt((ans[0] - x) * (ans[0] - x) + (ans[1] - y) * (ans[1] - y)) < Math.sqrt((ans[2] - x) * (ans[2] - x) + (ans[3] - y) * (ans[3] - y))) {
								jx = ans[0] - centerX;
								jy = ans[1] - centerY;
							} else {
								jx = ans[2] - centerX;
								jy = ans[3] - centerY;
							}
							var currentTouchX = event.touches[effectiveFinger].clientX - centerX;
							var currentTouchY = event.touches[effectiveFinger].clientY - centerY;
							var manualValueX=currentTouchX/josize*1.0*2.0;
							var manualValueY=currentTouchY/josize*1.0*2.0;
	
							if(manualValueX > 1.0)
							{
								manualValueX = 1.0;
							}
							else if(manualValueX < -1.0)
							{
								manualValueX = -1.0;
							}
	
							if(manualValueY > 1.0)
							{
								manualValueY = 1.0;
							}
							else if(manualValueY < -1.0)
							{
								manualValueY = -1.0;
							}
	
							moveX = manualValueX*objGlobalVariable.objCusUpdateTopState.variable_local_manual_v/100.0;
							moveY = manualValueY*objGlobalVariable.objCusUpdateTopState.variable_local_manual_v/100.0;
	
							moveX = moveX.toFixed(2);
							moveY = moveY.toFixed(2);
	
							isRockerMove = 1;
							//var msg = " x " + manualValueX.toFixed(2) + " y "+manualValueY.toFixed(2);
							//document.getElementById("showmsg").textContent=msg;
						}
						//move();
					event.preventDefault(); //防止页面滑动，取消掉默认的事件
					break;
			}
			
		}
                
                

		
	}
	requestAnimationFrame(move); //开始绘图
=======
    document.addEventListener('touchstart', touch, false);
    document.addEventListener('touchmove', touch, false);
    document.addEventListener('touchend', touch, false);

    //加载的时候先把摇杆绘制出来再说
    move();
    var effectiveFinger = -1; //当前有效手指
    //触摸事件触发函数
    function touch(event) {
        var event = event || window.event;


        var oInp = document.getElementById("inp");
        //界面未显示时使触摸事件无效
        var pageManualShowState = "";
        pageManualShowState = $("#page_manual_m_manual").css("display");
        if (pageManualShowState == "none" || pageManualShowState == "" || pageManualShowState == undefined)
        {


        } else
        {

            switch (event.type) {
                case "touchstart":
                    //判断是否击中摇杆头
                    //开启手动线程



                    for (var i = 0; i < event.touches.length; i = i + 1) {
                        if (Math.sqrt(Math.pow(event.touches[i].clientX - centerX - touchX, 2) +
                                Math.pow(event.touches[i].clientY - centerY - touchY, 2)) <=
                                josize / 2 - jisize / 2) {
                            effectiveFinger = i;


                            if (window.objGlobalManage.intervalSendCmd === 0)
                            {

                                window.objGlobalManage.intervalSendCmd = 1;
                                window.objGlobalManage.intervalTimer = setInterval(function () {
                                    sendRockerMotionCmd()
                                }, 100);
                            } else
                            {
                                stopRockerManualTimer();
                                window.objGlobalManage.intervalSendCmd = 1;
                                window.objGlobalManage.intervalTimer = setInterval(function () {
                                    sendRockerMotionCmd()
                                }, 100);
                            }

                            isRockerMove = 1;
                        }
                    }
                    break;
                case "touchend": //手指离开的时候
                    //若有效手指离开,那就把内摇杆放中间
                    if (event.touches[effectiveFinger] == null) {
                        if (event.touches[0] == null) {
                            jx = 0;
                            jy = 0;
                        }
                        effectiveFinger -= 1;
                        stopRockerManualTimer();
                        //var msg = " x " + 0 + " y "+0;
                        //	document.getElementById("showmsg").textContent=msg;
                    }
                    break;
                case "touchmove": //手指移动的时候：
                    //是否触摸点在摇杆上


                    if (effectiveFinger != -1)
                        if (Math.sqrt(Math.pow(event.touches[effectiveFinger].clientX - centerX - touchX, 2) +
                                Math.pow(event.touches[effectiveFinger].clientY - centerY - touchY, 2)) <=
                                josize / 2 - jisize / 2) {
                            jx = event.touches[effectiveFinger].clientX - centerX - touchX;
                            jy = event.touches[effectiveFinger].clientY - centerY - touchY;

                            var currentTouchX = event.touches[effectiveFinger].clientX - centerX;
                            var currentTouchY = event.touches[effectiveFinger].clientY - centerY;
                            var manualValueX = currentTouchX / josize * 1.0 * 2.0;
                            var manualValueY = currentTouchY / josize * 1.0 * 2.0;

                            if (manualValueX > 1.0)
                            {
                                manualValueX = 1.0;
                            } else if (manualValueX < -1.0)
                            {
                                manualValueX = -1.0;
                            }

                            if (manualValueY > 1.0)
                            {
                                manualValueY = 1.0;
                            } else if (manualValueY < -1.0)
                            {
                                manualValueY = -1.0;
                            }

                            moveX = manualValueX * window.objGlobalManage.objCusUpdateTopState.variable_local_manual_v / 100.0;
                            moveY = manualValueY * window.objGlobalManage.objCusUpdateTopState.variable_local_manual_v / 100.0;

                            moveX = moveX.toFixed(2);
                            moveY = moveY.toFixed(2);

                            isRockerMove = 1;
                            //var msg = currentTouchX.toFixed(2)+" " + currentTouchY.toFixed(2)+" " + manualValueX.toFixed(2) + " "+manualValueY.toFixed(2);
                            //document.getElementById("showmsg").textContent=msg;
                        } else
                                //否则计算摇杆最接近的位置
                                {
                                    var x = event.touches[effectiveFinger].clientX - touchX,
                                            y = event.touches[effectiveFinger].clientY - touchY,
                                            r = josize / 2 - jisize / 2;
                                    var ans = GetPoint(centerX, centerY, r, centerX, centerY, x, y);
                                    //圆与直线有两个交点，计算出离手指最近的交点
                                    if (Math.sqrt((ans[0] - x) * (ans[0] - x) + (ans[1] - y) * (ans[1] - y)) < Math.sqrt((ans[2] - x) * (ans[2] - x) + (ans[3] - y) * (ans[3] - y))) {
                                        jx = ans[0] - centerX;
                                        jy = ans[1] - centerY;
                                    } else {
                                        jx = ans[2] - centerX;
                                        jy = ans[3] - centerY;
                                    }
                                    var currentTouchX = event.touches[effectiveFinger].clientX - centerX;
                                    var currentTouchY = event.touches[effectiveFinger].clientY - centerY;
                                    var manualValueX = currentTouchX / josize * 1.0 * 2.0;
                                    var manualValueY = currentTouchY / josize * 1.0 * 2.0;

                                    if (manualValueX > 1.0)
                                    {
                                        manualValueX = 1.0;
                                    } else if (manualValueX < -1.0)
                                    {
                                        manualValueX = -1.0;
                                    }

                                    if (manualValueY > 1.0)
                                    {
                                        manualValueY = 1.0;
                                    } else if (manualValueY < -1.0)
                                    {
                                        manualValueY = -1.0;
                                    }

                                    moveX = manualValueX * window.objGlobalManage.objCusUpdateTopState.variable_local_manual_v / 100.0;
                                    moveY = manualValueY * window.objGlobalManage.objCusUpdateTopState.variable_local_manual_v / 100.0;

                                    moveX = moveX.toFixed(2);
                                    moveY = moveY.toFixed(2);

                                    isRockerMove = 1;
                                    //var msg = " x " + manualValueX.toFixed(2) + " y "+manualValueY.toFixed(2);
                                    //document.getElementById("showmsg").textContent=msg;
                                }
                    //move();
                    event.preventDefault(); //防止页面滑动，取消掉默认的事件
                    break;
            }

        }




    }
    requestAnimationFrame(move); //开始绘图
>>>>>>> dev
}

//计算圆于直线的交点（这一块好难啊）
//圆的坐标为cx，cy 半径为r
//直线上两点的坐标分别为(stx,sty)、(edx,edy)
function GetPoint(cx, cy, r, stx, sty, edx, edy) {
<<<<<<< HEAD
	//(x-cx)^2+(y-cy)^2=r^2
	//y=k*x+b
	var k = (edy - sty) / (edx - stx);
	var b = edy - k * edx;
	//(1 + k^2)*x^2 - x*(2*cx -2*k*(b -cy) ) + cx*cx + ( b - cy)*(b - cy) - r*r = 0
	var x1, y1, x2, y2;
	var c = cx * cx + (b - cy) * (b - cy) - r * r;
	var a = (1 + k * k);
	var b1 = (2 * cx - 2 * k * (b - cy));

	var tmp = Math.sqrt(b1 * b1 - 4 * a * c);

	x1 = (b1 + tmp) / (2 * a);
	y1 = k * x1 + b;

	x2 = (b1 - tmp) / (2 * a);
	y2 = k * x2 + b;
	return [x1, y1, x2, y2];
}


  //获取控件左绝对位置
  function getAbsoluteLeft(pElementId) {
	o = document.getElementById(pElementId);
	oLeft = o.offsetLeft;            
	while(o.offsetParent!=null) { 
		oParent = o.offsetParent;    
		oLeft += oParent.offsetLeft; 
		o = oParent;
	}
	return oLeft;
}
//获取控件上绝对位置
function getAbsoluteTop(pElementId) {
	o = document.getElementById(pElementId);
	oTop = o.offsetTop;
	while(o.offsetParent!=null)
	{  
		oParent = o.offsetParent ;
		oTop += oParent.offsetTop ; // 加父层级顶部位置
		o = oParent ;
	}
	return oTop ;
}


function sendRockerMotionCmd(){
	/* 停止运行 */
	
	if(objGlobalVariable.rockerMotionCmd === 0)
	{
		return false;
	}


	var windowIsShow = getVisibilityState();
	
	//浏览器隐藏时停止发送
	if(windowIsShow === "visibilityState")
	{

	}
	else
	{
		stopRockerManualTimer();
		return false;
	}

	//websocket消息发送
	var cmdRowParent = {};
	var cmdRowChild = {};
	cmdRowChild.move_x=-moveY;
	cmdRowChild.move_y=0.0;
	cmdRowChild.move_rotate=-moveX;

	cmdRowParent.manual_move = cmdRowChild;
	var sendData=JSON.stringify(cmdRowParent);
	$("#manual_show_text").text(sendData);
	
	objGlobalVariable.objWebSocket.sendMessage(sendData);

}

function stopRockerManualTimer(){
        
	objGlobalVariable.rockerMotionCmd = 0;
	
	clearInterval(objGlobalVariable.intervalRockerSendCmd);
	stopRockerMotionCmd();
}

/* 停止 */
function stopRockerMotionCmd(){

	
	if(isRockerMove === 1)
	{

		var cmdRowParent = {};
		var cmdRowChild = {};
		cmdRowChild.parameter=0;
		cmdRowParent.manual_stop = cmdRowChild;
		var sendData=JSON.stringify(cmdRowParent);
		$("#manual_show_text").text(sendData);
		objGlobalVariable.objWebSocket.sendMessage(sendData);
		isRockerMove = 0;
	}
	
=======
    //(x-cx)^2+(y-cy)^2=r^2
    //y=k*x+b
    var k = (edy - sty) / (edx - stx);
    var b = edy - k * edx;
    //(1 + k^2)*x^2 - x*(2*cx -2*k*(b -cy) ) + cx*cx + ( b - cy)*(b - cy) - r*r = 0
    var x1, y1, x2, y2;
    var c = cx * cx + (b - cy) * (b - cy) - r * r;
    var a = (1 + k * k);
    var b1 = (2 * cx - 2 * k * (b - cy));

    var tmp = Math.sqrt(b1 * b1 - 4 * a * c);

    x1 = (b1 + tmp) / (2 * a);
    y1 = k * x1 + b;

    x2 = (b1 - tmp) / (2 * a);
    y2 = k * x2 + b;
    return [x1, y1, x2, y2];
}


//获取控件左绝对位置
function getAbsoluteLeft(pElementId) {
    o = document.getElementById(pElementId);
    oLeft = o.offsetLeft;
    while (o.offsetParent != null) {
        oParent = o.offsetParent;
        oLeft += oParent.offsetLeft;
        o = oParent;
    }
    return oLeft;
}
//获取控件上绝对位置
function getAbsoluteTop(pElementId) {
    o = document.getElementById(pElementId);
    oTop = o.offsetTop;
    while (o.offsetParent != null)
    {
        oParent = o.offsetParent;
        oTop += oParent.offsetTop; // 加父层级顶部位置
        o = oParent;
    }
    return oTop;
}


function sendRockerMotionCmd() {
    
    var windowIsShow = getVisibilityState();

    //浏览器隐藏时停止发送
    if (windowIsShow === "visibilityState")
    {

    } else
    {
        stopRockerManualTimer();
        return false;
    }
    //手动运行判断是否是T1T2模式
    if( (window.objGlobalManage.objCusUpdateTopState.variable_state_mode !== enumTypeMode.kModeT1) 
            && (window.objGlobalManage.objCusUpdateTopState.variable_state_mode !== enumTypeMode.kModeT2) )
    {
        window.objGlobalManage.tipsManualMustBeT1T2 = 1;
        $("#top_right_state_modle").trigger("click");
        return false;
    }
    //如果伺服关闭就提示开启
    if(window.objGlobalManage.objCusUpdateTopState.variable_state_servo === enumTypeServo.kServoClose)
    {
        $("#top_right_state_servo").trigger("click");
        return false;
    }

    var mx = -moveY;
    var my = 0.0;
    var mr = -moveX;
    window.objGlobalManage.objCusProtocol.func_manual_move(mx,my,mr);

}

function stopRockerManualTimer() {

    clearInterval(window.objGlobalManage.intervalTimer);
    window.objGlobalManage.intervalSendCmd = 0;
    stopRockerMotionCmd();
}

/* 停止 */
function stopRockerMotionCmd() {
            
    if (isRockerMove === 1)
    {
        window.objGlobalManage.objCusProtocol.func_manual_stop();
        isRockerMove = 0;
    }

>>>>>>> dev
}
